package com.alexgaoyh.MutiModule.Dubbo.RWSeperate.consumer.web.demo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.MutiModule.common.myPage.MyPageView;
import com.MutiModule.common.myPage.MyQueryResult;
import com.MutiModule.common.utils.StringUtilss;
import com.MutiModule.common.vo.Result;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.consumer.web.base.BaseController;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.consumer.web.demo.util.DemoEntityVOUtilss;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.consumer.web.demo.vo.DemoEntity;

@Controller
@RequestMapping(value="manager/sitemesh")
public class DemoEntityPageViewController extends BaseController{

	@RequestMapping(value = "/index")
	public ModelAndView index(ModelAndView model, HttpServletRequest request) {
		
		model.setViewName("demo/sitemesh/index");
		return model;
	}
	
	
	@RequestMapping(value = "/list")
	public ModelAndView list(ModelAndView model, HttpServletRequest request) {

		List<DemoEntity> list = DemoEntityVOUtilss.generateList();
		
		String currentPageStr = request.getParameter("currentPage");
		if(StringUtilss.isEmpty(currentPageStr)) {
			currentPageStr = "1";
		}
		
		int currentPage = Integer.parseInt(currentPageStr);//当前页数
		int totalCount = list.size();
		int recordPerPage = 10;//每页显示条数
		
		MyPageView<DemoEntity> pageView = new MyPageView<DemoEntity>(recordPerPage, currentPage);
		int start = pageView.getStartRecordIndex() > totalCount ? totalCount : pageView.getStartRecordIndex();
		int lenght = pageView.getRecordPerPage();
		
		int end = ((start + lenght) >= totalCount) ? totalCount : (start + lenght);
		
		MyQueryResult<DemoEntity> qr = new MyQueryResult<DemoEntity>(list.subList(
				start, end), list.size());
		pageView.setQueryResult(qr);

		model.addObject("pageView", pageView);
		
		model.setViewName("demo/sitemesh/list");
		return model;
	}
}
